package service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import Dao.LoginDao;
import bean.LoginResult;

/**
 * Servlet implementation class loginService
 */
public class loginService extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public loginService() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		LoginDao loginDao = new LoginDao();
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("userPwd");
		List<Object> params = new ArrayList<Object>();
		params.add(userName);
		params.add(passWord);
		LoginResult loginResult = loginDao.startLogin(params);
		try {
			if(loginResult.getResultCode()==1){
			request.getSession().setAttribute("loginResult", loginResult);
			}
			JSONObject json = JSONObject.fromObject(loginResult);
			writer.println(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		writer.flush();
		writer.close();

	}

}
